package com.yyx.app1.My_function.UI;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.yyx.app1.Data.BOSSInfo;
import com.yyx.app1.Data.BaseDataManager;
import com.yyx.app1.Data.DataInit;
import com.yyx.app1.Data.WebOperation;
import com.yyx.app1.Data.testDate;
import com.yyx.app1.Interface.Trancaction;
import com.yyx.app1.MyApp;
import com.yyx.app1.R;
import com.yyx.app1.tool.JsonObjectPostRequest;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Administrator on 2015/10/22.
 */
@EActivity(R.layout.activity_register)
public class Register_Activity extends Activity {
    @ViewById
    EditText phone,password,smscode;

    @Click
    public void home()
    {
        finish();
    }

    @Click
    public void activity_register_button()
    {
        final String phonestr = phone.getText().toString().trim();
        String passwordstr = password.getText().toString().trim();
        String code = smscode.getText().toString().trim();
        if (passwordstr.isEmpty() || phonestr.isEmpty() || code.isEmpty())
            return;
        HashMap<String, String> map = new HashMap<>();
        map.put("mobile",phonestr);
        map.put("password",passwordstr);
        map.put("smsCode",code);
        JsonObjectPostRequest.HttpPost(this, map,"appRegisterA" , new Trancaction() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    Log.d("正常", response.toString());
                    String message = response.getString("message");
                    int statusCode = response.getInt("statusCode");
                    if (statusCode == 200) {
                        Toast.makeText(Register_Activity.this, message, Toast.LENGTH_SHORT).show();
                        ((MyApp)Register_Activity.this.getApplication()).setBossInfo(BOSSInfo.setBossInfo(response));
                        startActivity(new Intent(Register_Activity.this, My_Acitivity_.class));
                    } else {
                        Toast.makeText(Register_Activity.this, message, Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Log.e("异常1511031854", e.getMessage());
                }
            }

            @Override
            public Request.Priority getPriority() {
                return Request.Priority.HIGH;
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Click
    public void getcode()
    {
        String str = phone.getText().toString().trim();
        if (str.isEmpty() || str.length()!=11) {
            Toast.makeText(this,"输入11位完整手机号码",Toast.LENGTH_SHORT).show();
            return;
        }
//        WebOperation.JsonRequest(this, "sendSMSCodeForRegister", new String[]{"mobile"}, new String[]{str}, new Trancaction() {
//            @Override
//            public void onResponse(JSONObject response) {
//                try {
//                    Log.d("正常", response.toString());
//                    String message = response.getString("message");
//                    Toast.makeText(Register_Activity.this, "验证码"+message, Toast.LENGTH_LONG).show();
//                }
//                catch (Exception e)
//                {
//                    Log.e("异常1511031817", e.getMessage());
//                }
//            }
//
//            @Override
//            public Request.Priority getPriority() {
//                return null;
//            }
//
//            @Override
//            public void onErrorResponse(VolleyError error) {
//
//            }
//        });

        //////////////////////////////////////////
        HashMap<String, String> mMap = new HashMap<>();
        mMap.put("mobile",str);
        JsonObjectPostRequest.HttpPost(this, mMap, "sendSMSCodeForRegister", new Trancaction() {
            @Override
            public void onResponse(JSONObject response) {
                String message = null;
                try {
                    message = response.getString("message");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Toast.makeText(Register_Activity.this,message,Toast.LENGTH_SHORT).show();
            }

            @Override
            public Request.Priority getPriority() {
                return null;
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            home();
        }
        return true;
    }
    public String assignment(String key,JSONObject response) {
        try {
            String name = response.getString(key);
            if (name == null || name.isEmpty()) {
                name = "未知";
                if (key.equals("portraitURL"))
                    name=testDate.portrait;
            }
            return name;
        }catch (Exception e)
        {
            Log.e("异常1511081318", e.toString());
            return "未知";
        }
    }
}
